package com.sugar.FileListener.config;

import com.sugar.FileListener.listener.FileWatchEventListener;
import com.sugar.FileListener.service.FileWatcherService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import javax.annotation.PreDestroy;

/**
 * @author felix
 * @date 2025/7/3 15:11
 */
@Configuration
@EnableConfigurationProperties(FileWatcherProperties.class)
@ConditionalOnProperty(name = "file-watcher.enabled", havingValue = "true")
public class FileWatcherAutoConfiguration {

    private FileWatcherService service;

    @Bean
    public FileWatcherService fileWatcherService(FileWatcherProperties props,
                                                 FileWatchEventListener listener) throws Exception {
        service = new FileWatcherService(props, listener);
        service.start();
        return service;
    }

    @PreDestroy
    public void stop() throws Exception {
        if (service != null) service.stop();
    }
}
